package com.example.service.impl;

import com.example.mapper.ArticleMapper;
import com.example.mapper.InteractMapper;
import com.example.pojo.Article;
import com.example.pojo.Favorite;
import com.example.pojo.Like;
import com.example.service.InteractService;
import com.example.utils.ThreadLocalUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.time.LocalDateTime;
import java.util.List;

@Service
public class InteractServiceImpl implements InteractService {
//    这里的id为文章id
    @Autowired
    private InteractMapper interactMapper;
    @Autowired
    private ArticleMapper articleMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void like(Integer id) {
        Claims claims = ThreadLocalUtil.get();
        Integer userid = (Integer) claims.get("id");
        //查看是否有点过赞
        Like like = interactMapper.ifliked(id,userid);
        if (like != null){
            like.setUpdateTime(LocalDateTime.now());
            like.setStatus(1);
            interactMapper.updateStatus(like);
        }
        else {
            like = new Like();
            like.setArticleId(id);
            like.setUserId(userid);
            like.setUpdateTime(LocalDateTime.now());
            like.setStatus(1);
            interactMapper.addlike(like);
            Article article = new Article();
            article = articleMapper.detail(id);
            article.setLikes(article.getLikes()+1);
            articleMapper.update(article);
        }
    }

    @Override
    public void collect(Integer id) {
        Favorite favorite = new Favorite();
        Claims claims = ThreadLocalUtil.get();
        Integer userid = (Integer) claims.get("id");
        favorite.setUserId(userid);
        favorite.setArticleId(id);
        favorite.setCreateTime(LocalDateTime.now());
        interactMapper.collect(favorite);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void unlike(Integer id) {
        LocalDateTime updateTime = LocalDateTime.now();
        Claims claims = ThreadLocalUtil.get();
        Integer userid = (Integer) claims.get("id");
        Article article = new Article();
        article = articleMapper.detail(id);
        article.setLikes(article.getLikes()-1);
        articleMapper.update(article);
        interactMapper.unlike(id,userid,updateTime);
    }

    @Override
    public void uncollect(Integer id) {
        Claims claims = ThreadLocalUtil.get();
        Integer userid = (Integer) claims.get("id");
        interactMapper.uncollect(id,userid);
    }

    @Override
    public List<Article> mylike() {
        Claims claims = ThreadLocalUtil.get();
        Integer userid = (Integer) claims.get("id");
        List<Article> articleList =  interactMapper.mylike(userid);
        return articleList;
    }

    @Override
    public List<Article> mycollect() {
        Claims claims = ThreadLocalUtil.get();
        Integer userid = (Integer) claims.get("id");
        List<Article> articleList = interactMapper.mycollect(userid);
        return articleList;
    }
}
